package com.sxtyxt.online.me;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.sxtyxt.entity.ResultVo;
import com.sxtyxt.online.ActBase;
import com.sxtyxt.online.R;
import com.sxtyxt.online.SystemConst;
import com.sxtyxt.util.GsonUtil;
import com.sxtyxt.volley.LoadingDialogResultListenerImpl;
import com.sxtyxt.volley.URL;
import com.sxtyxt.volley.VolleyUtils;
import com.sxtyxt.widget.ClearEditText;

/**
 * 修改密码
 * Created by General on 15/8/26.
 */
public class ActModifyPwd extends ActBase {

    private ClearEditText etxtOld, etxtNew, etxtNewConfirm;

    @Override
    protected int getLayoutId() {
        return R.layout.modify_pwd;
    }

    @Override
    protected void initUI() {
        super.initUI();
        etxtOld = (ClearEditText) findViewById(R.id.etxt_old_pwd);
        etxtNew = (ClearEditText) findViewById(R.id.etxt_new_pwd);
        etxtNewConfirm = (ClearEditText) findViewById(R.id.etxt_new_pwd_confirm);

        Button btnOk = (Button) findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(getStringByUI(etxtOld))) {
                    doToast("旧密码不能为空");
                    return;
                }
                if (getStringByUI(etxtOld).length() < 6 || getStringByUI(etxtOld).length() > 20) {
                    doToast("旧密码应该为6-20位");
                    return;
                }

                if (getStringByUI(etxtNew).length() < 6 || getStringByUI(etxtNew).length() > 20) {
                    doToast("新密码应该为6-20位");
                    return;
                }

                if (TextUtils.isEmpty(getStringByUI(etxtNew))) {
                    doToast("新密码不能为空");
                    return;
                }
                if (!getStringByUI(etxtNew).equals(getStringByUI(etxtNewConfirm))) {
                    doToast("两次输入的密码不一致");
                    return;
                }
                Log.e("++++++++++++++",getStringByUI(etxtOld));
                Log.e("++++++++++++++",getStringByUI(etxtNew));
                Log.e("++++++++++++++",getLoginUser().getPkId());
                requestService();

            }
        });

    }

    private void requestService() {
        if (getStringByUI(etxtOld)!=null&&getStringByUI(etxtNew)!=null&&getLoginUser().getPkId()!=null) {
            VolleyUtils.requestService(SystemConst.MODIFY_PWD, URL.modifyPwd(getLoginUser().getPkId(),getStringByUI(etxtOld),
                    getStringByUI(etxtNew)), new LoadingDialogResultListenerImpl(this, "正在修改密码") {


                @Override
                public void onSuccess(String response) {
                    super.onSuccess(response);
                    ResultVo<?> vo = GsonUtil.deser(response, ResultVo.class);
                    if (vo == null) {
                        return;
                    }
                    if (vo.getResult() == 1) {
                        doToast("修改成功");
                        finish();
                    } else {
                        doToast(vo.getMsg());
                    }
                }

                @Override
                public void onError() {
                    super.onError();
                }
            });
        }

    }


}
